/*
 * Copyright (c) 2024 坚果派
 * 
 * 公众号：nutpi
 * 电话：17752170152
 * 官网：https://www.nutpi.net/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_digital_timer_depo/models/time_format.dart';

class DigitalClock extends StatefulWidget {
  final TimeFormat timeFormat;
  final double fontSize;
  final Color textColor;

  const DigitalClock({
    Key? key,
    this.timeFormat = TimeFormat.arabic,
    this.fontSize = 60.0,
    this.textColor = Colors.black,
  }) : super(key: key);

  @override
  State<DigitalClock> createState() => _DigitalClockState();
}

class _DigitalClockState extends State<DigitalClock> {
  late Timer _timer;
  late DateTime _currentTime;

  @override
  void initState() {
    super.initState();
    _currentTime = DateTime.now();
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        _currentTime = DateTime.now();
      });
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  String _formatTimeArabic() {
    return '${_currentTime.hour.toString().padLeft(2, '0')}:${_currentTime.minute.toString().padLeft(2, '0')}:${_currentTime.second.toString().padLeft(2, '0')}';
  }

  String _formatTimeChinese() {
    final chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    final chineseUnits = ['', '十', '百'];

    String formatNumber(int number) {
      if (number == 0) return chineseNums[0];

      String result = '';
      int temp = number;
      int unitIndex = 0;
      bool hasZero = false;

      while (temp > 0) {
        int digit = temp % 10;
        if (digit != 0) {
          if (hasZero) {
            result = chineseNums[0] + result;
            hasZero = false;
          }
          if (digit == 1 && unitIndex == 1 && temp ~/ 10 == 0) {
            result = chineseUnits[unitIndex] + result;
          } else {
            result = chineseNums[digit] + chineseUnits[unitIndex] + result;
          }
        } else {
          hasZero = true;
        }
        temp ~/= 10;
        unitIndex++;
      }

      return result;
    }

    return '${formatNumber(_currentTime.hour)}时${formatNumber(_currentTime.minute)}分${formatNumber(_currentTime.second)}秒';
  }

  @override
  Widget build(BuildContext context) {
    return Text(
      widget.timeFormat == TimeFormat.arabic
          ? _formatTimeArabic()
          : _formatTimeChinese(),
      style: TextStyle(
        fontSize: widget.fontSize,
        color: widget.textColor,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}
